﻿using BCT.Models;
using BCT.Repositories.DataAccess;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BCT.Repositories
{
    public class RoleManageRepository : IRoleManageRepository
    {
        //获得Role列表
        public List<RoleModel> GetRoleList(string rolename,string roledescription)
        {
            using (var context = new EvaluateEntities()) {
                if (string.IsNullOrEmpty(rolename) && string.IsNullOrEmpty(roledescription))
                {
                    var roleList = context.Role
                                .Select(s => new RoleModel()
                                {
                                    RoleId = s.RoleId,
                                    RoleName = s.RoleName,
                                    RoleDescription = s.RoleDescription,
                                    Active = s.Active
                                }).ToList();
                    return roleList;
                }
                else if ((!string.IsNullOrEmpty(rolename)) && (!string.IsNullOrEmpty(roledescription)))
                {
                    var roleList = context.Role
                                .Where(s => s.RoleName.Contains(rolename) && s.RoleDescription.Contains(roledescription))
                                .Select(s => new RoleModel()
                                {
                                    RoleId = s.RoleId,
                                    RoleName = s.RoleName,
                                    RoleDescription = s.RoleDescription,
                                    Active = s.Active
                                }).ToList();
                    return roleList;
                }
                else {
                    var roleList = context.Role
                                .Where(s => s.RoleName.Contains(rolename)|| s.RoleDescription.Contains(roledescription))
                                .Select(s => new RoleModel()
                                {
                                    RoleId = s.RoleId,
                                    RoleName = s.RoleName,
                                    RoleDescription = s.RoleDescription,
                                    Active = s.Active
                                }).ToList();
                    return roleList;
                }                
            }              
        }

        //获取Role的下拉列表
        public List<RoleModel> GetRoleSelect()
        {
            using (var context = new EvaluateEntities())
            {
                var rolelist = context.Role
                               .Where(s => s.Active == true)
                               .Select(s => new RoleModel()
                               {
                                   RoleId = s.RoleId,
                                   RoleName = s.RoleName,
                                   RoleDescription = s.RoleDescription,
                                   Active = s.Active
                               }).ToList();
                return rolelist;
            }
        }

        //添加角色
        public bool AddRole(RoleModel model)
        {
            using (var context = new EvaluateEntities()) {
                var role = context.Role.Where(s => s.RoleName == model.RoleName).ToList();
                if (!string.IsNullOrEmpty(model.RoleName) && role.Count == 0)
                {
                    context.Role.Add(new Role()
                    {
                        RoleName = model.RoleName,
                        RoleDescription = model.RoleDescription,
                    });
                   return  context.SaveChanges()>0;
                }
                return false;
            }
        }

        //修改角色
        public string ModefyRole(RoleModel model)
        {
            using (var context = new EvaluateEntities())
            {
                var role = context.Role.FirstOrDefault(s => s.RoleId == model.RoleId);
                if (role != null)
                {
                    role.RoleName = model.RoleName;
                    role.RoleDescription = model.RoleDescription;
                }            
                if(context.SaveChanges() > 0)
                {
                    return "修改成功！";
                }
                else
                {
                    return "修改失败！";
                }
            }
        }

        //禁用或启用角色
        public string LogDelete(RoleModel model)
        {
            using (var context = new EvaluateEntities())
            {
                    var role = context.Role.FirstOrDefault(s => s.RoleId == model.RoleId);
                    role.Active = model.Active;
                    if (context.SaveChanges()>0)
                    {
                        return "修改成功！";
                    }
                    else
                    {
                        return "修改失败！";
                    }

            }
        }
    }
}
